package com.gs.synedocket.util;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gs.synedocket.R;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.dialog.TermDialog;

import java.util.UUID;

public class StringUtil {
    public static void setClick(Context context, TextView tvAgree, TermDialog termDialog) {
        String text = context.getString(R.string.agree_text);
        String terms = context.getString(R.string.terms);
        SpannableString ss = new SpannableString(text + terms);
        ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                LogUtil.getInstance().insertOrReplaceData("查看《銷售條款》");
                termDialog.show();
            }
        }, text.length(), ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvAgree.setText(ss);
        tvAgree.setMovementMethod(LinkMovementMethod.getInstance());
    }

    /**
     * 判断图片是否为本地未上传图片
     *
     * @param url
     * @return
     */
    public static boolean isLocalPhoto(String url) {
        return url.contains(AllConstants.PHOTO_NAME);
    }

    /**
     * 判断图片是否为网络图片
     *
     * @param url
     * @return
     */
    public static boolean isNetPhoto(String url) {
        return url.startsWith(OKHttpUtil.IMG_URL);
    }

    public static String getGUID() {
        return UUID.randomUUID().toString();
    }

    public static int getOffset(int pageInx) {
        return (pageInx - 1) * 10;
    }

    public static int getLimit() {
        return 10;
    }

    public static String getNumString(String str, int num) {
        return String.format("%1$-" + num + "s", str);
    }

    public static String getString(int resource) {
        return AppUtil.getInstance().getContext().getResources().getString(resource);
    }

    public static String getString(int resource, Object... args) {
        return AppUtil.getInstance().getContext().getResources().getString(resource, args);
    }
}
